package com.blog.video.controller;

import com.blog.video.pojo.Article;
import com.blog.video.pojo.Video;
import com.blog.video.service.ArticleService;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * 网站首页
 */
@Controller
@RequestMapping("")
public class IndexController {

    @Autowired
    private ArticleService articleService;

    @GetMapping("/kk")
    public String kk(Map<String, Object> map) {
        List<Video> list = new ArrayList<>();
        for (int i = 0; i < 60; i++) {
            Video video=new Video();
            String numeric = RandomStringUtils.randomNumeric(4);
            video.setNum(numeric);
            list.add(video);
        }
        map.put("num", list);
        return "kkkkk/kkk";
    }

    @GetMapping("/random")
    @ResponseBody
    public String random() {
        String numeric = RandomStringUtils.randomNumeric(6);
        return numeric;
    }

    /**
     * 首页文章
     */
    @GetMapping("/index")
    public String index(Map<String, Object> map) {
        List<Article> list = articleService.findAll(1,100);
        map.put("list", list);
        return "index";
    }

    /**
     * 文章详情页
     * @param id id
     * @param map map
     * @return 返回页面
     */
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") String id, Map<String, Object> map) {
        Article article = articleService.findById(id);
        map.put("article", article);
        return "detail";
    }

    /**
     * 归档
     * @param map
     * @return
     */
    @GetMapping("/guidang")
    public String guidang(Map<String, Object> map) {
        List<Article> list = articleService.findAll(1,100);
        map.put("list", list);
        return "guidang";
    }

}
